package top.z.orm.handle.osql.name;

public class NameUtil {

    public static String getClassFullName(Object obj) {
        // include package name
        return obj.getClass().getName();
    }

    public static String getClassName(Object obj) {
        return obj.getClass().getSimpleName();
    }

    /**
     * 小驼峰转下划线
     *
     * @param name name
     * @return 下划线字符串
     * eg: beeName->bee_name,beeTName->bee_t_name
     */
    public static String toUnderscore(String name) {
        StringBuilder buf = new StringBuilder(name);
        for (int i = 1; i < buf.length() - 1; i++) {
            if (Character.isUpperCase(buf.charAt(i))) {
                buf.insert(i++, '_');
            }
        }
        return buf.toString().toLowerCase();
    }

    /**
     * 下划线转驼峰
     *
     * @param name name
     * @return 驼峰字符串
     * eg:  bee_name->beeName,bee_t_name->beeTName
     */
    public static String toCamelcase(String name) {
        StringBuilder buf = new StringBuilder(name.trim());
        char          temp;
        for (int i = 1; i < buf.length() - 1; i++) {
            temp = buf.charAt(i);
            if (buf.charAt(i) == '_') {
                buf.deleteCharAt(i);
                temp = buf.charAt(i);
                if (temp >= 'a' && temp <= 'z')
                    buf.setCharAt(i, (char) (temp - 32));
            }
        }
        return buf.toString();
    }

    /**
     * 首字母转换成大写
     */
    public static String firstLetterToUpperCase(String str) {
        String result;
        if (str.length() > 1) {
            result = str.substring(0, 1).toUpperCase() + str.substring(1);
        } else {
            result = str.toUpperCase();
        }
        return result;
    }

    /**
     * 首字母转换成小写
     */
    public static String firstLetterToLowerCase(String str) {
        String result;
        if (str.length() > 1) {
            result = str.substring(0, 1).toLowerCase() + str.substring(1);
        } else {
            result = str.toLowerCase();
        }
        return result;
    }
}